<?php
namespace app\vueapi\controller;

use app\common\model\{
    College as CollegeModel
};

use think\Db;

class College extends Base
{
    public function index()
    {
        $data = $this->request->param();
        $page = $data['pageSize'] ?? 1000;
        if(!empty($data['name'])){
            $res = CollegeModel::where('name',$data['name'])->field('id , name')->paginate($page);
            return json(msg(1, $res));
        }else {
            $res = CollegeModel::field('id , name')->paginate($page);
            return json(msg(1, $res));
        }
    }

    public function seek()
    {
        $data = $this->request->param();
        $page = $data['pageSize'] ?? 10;
        if(!empty($data['name'])){
            $where['name'] = ['like', "%{$data['name']}%"];
        }else{
            return json(msg(0, '', '请输入查询的学院'));
        }
        $field = 'id, name';
        $res = CollegeModel::where($where)->field($field)->paginate($page);
        return json(msg(1, $res));
    }

    public function addCollege()
    {
        $data = $this->request->param();
        if(!empty($data['name'])){
            $res = CollegeModel::where('name',$data['name'])->select();
            if($res != []) {
                return json(msg(0, '', '该学院已存在！'));
            }else{
                $res = CollegeModel::insert($data);
                return returnRes($res,'添加'); 
            }
        }else {
            return json(msg(0, '', '学院名不能为空！'));
        }
    }

    public function delCollege()
    {
        $data = $this->request->param();
        //不允许有相同的属性
        $college = CollegeModel::where('id', $data['id'])->find();
        if(empty($college)){
            return json(msg(0, '', '该学院不存在'));
        }
        // $have_sku = ProductSku::where('sku_id_1|sku_id_2', $data['id'])->find();
        // if($have_sku){
        //     return json(msg(0, '', '该属性已被使用, 无法进行删除'));
        // }
        $res = $college->delete();
        return returnRes($res, '删除学院');
    }
}

?>